package com.cf.main;

import java.util.List;

public class PearsonCorrelation implements Base {

	public double pearsonCorrelation(List<Integer> a, List<Integer> b) {
		int n = a.size();
		int sum_X = 0;
		int sum_Y = 0;
		int sum_squareX = 0;
		int sum_squareY = 0;
		int sum_product = 0;
		for (int i = 0; i < n; i++) {
			sum_X += a.get(i);
			sum_Y += b.get(i);
			sum_squareX += Math.pow(a.get(i), 2);
			sum_squareY += Math.pow(b.get(i), 2);
			sum_product += a.get(i) * b.get(i);
		}
		double sum = n * sum_product - sum_X * sum_Y;
		//double den = Math.sqrt((n * sum_squareX - Math.pow(sum_squareX, 2)) * (n * sum_squareY - Math.pow(sum_squareY, 2)));
		double den = Math.sqrt((n * sum_squareX - Math.pow(sum_X, 2)) * (n * sum_squareY - Math.pow(sum_Y, 2)));
		double result;
		if (den == 0)
			result = 0;
		else
			result = sum / den;
		return result;
	}

}
